package edu.fiu.cis.selfhealingdisk;

import java.util.ArrayList;
import java.util.List;

/**
 * @author vanvorst
 *
 */
public class ParityGroup {
	private final long id;
	private final List<BlockUnit> updates;
	private final List<BlockUnit> allUnits;

	/**
	 * @param id
	 */
	public ParityGroup(final long id) {
		this.id=id;
		this.allUnits = new ArrayList<BlockUnit>();
		this.updates = new ArrayList<BlockUnit>();
	}
	
	/**
	 * @return the numDataUpdatesSinceParityUpdate
	 */
	public final long getNumDataUpdatesSinceParityUpdate() {
		return updates.size();
	}
	
	/**
	 * @param unit
	 */
	public void updateDataUnit(final BlockUnit unit) {
		this.updates.add(unit);
	}
	
	/**
	 * @return the id
	 */
	public final long getId() {
		return id;
	}
	
	/**
	 * @param lastUpdate
	 */
	public void synchronizeParity(final long lastUpdate) {
		for(BlockUnit u: this.updates) {
			u.setLastUpdate(lastUpdate);
		}
		this.updates.clear();
	}
	
	/**
	 * Add a data unit
	 * 
	 * @param e
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 */
	public boolean add(BlockUnit e) {
		return allUnits.add(e);
	}

	/**
	 * @return
	 * @see java.util.List#size()
	 */
	public int size() {
		return allUnits.size();
	}
	
	/**
	 * Get a copy of the data units for this group
	 * @return
	 */
	public List<BlockUnit> getDataUnits() {
		List<BlockUnit> rv = new ArrayList<BlockUnit>();
		for(BlockUnit u : this.allUnits)
			if(u.getType()!=BlockUnit.BlockTypeEnum.DATA_BLOCK)
				rv.add(u);
		return rv;
	}
}